package com.gzhryc.shared_device.oem.code.agent.services;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.JdbcSession;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.oem.code.agent.dao.AgentLevelDao;
import com.gzhryc.shared_device.oem.code.agent.dao.db.AgentLevel;
import com.gzhryc.shared_device.oem.code.agent.services.dto.AgentLevelSearch;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class AgentLevelService {

    static Logger log = Logger.getLogger(AgentLevelService.class);

    public static AgentLevelService self() {
        return MultiDBTools.getService(AgentLevelService.class, true);
    }

    public static AgentLevelService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, AgentLevelService.class, true);
    }

    AgentLevelDao dao;

    public AgentLevelService(String jdbcKey) {
        this.dao = new AgentLevelDao(jdbcKey);
    }

    public List<AgentLevel> findBySearch(AgentLevelSearch search){
        Conditions conditions = dao.buildConditions(search);
        try {
            return dao.findByCondition(conditions);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public List<AgentLevel> findAll(){
        try {
            return dao.findAll();
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public AgentLevel getByNum(Integer num){
        try {
            return dao.getById(num);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public Integer getByMaxNum(){
        try {
            Integer num =  dao.getMaxNum();
            if(num != null){
                return num;
            }
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return 0;
    }
    
    public AgentLevel getByName(String name) {
    	if(StringTools.isNotBlank(name)) {
    		Conditions condition = new Conditions(AgentLevel.class);
    		condition.and().is("name", name);
    		
    		try {
				return dao.get(condition);
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
    	}
    	return null;
    }

    public synchronized boolean add(AgentLevel agentLevel, boolean jumpInLine) throws LogicException {
    	AgentLevel temp = getByNum(agentLevel.getNum());
        if(temp != null){
            throw new LogicException(agentLevel.getNum() + "级别已存在");
        }
        
        temp = getByName(agentLevel.getName());
        if(temp != null){
            throw new LogicException("级别名称已存在");
        }
    	
        if(jumpInLine){
            Conditions conditions = new Conditions(AgentLevel.class);
            conditions.and().ge("num",agentLevel.getNum());

            AgentLevel updateEntity = new AgentLevel();
            updateEntity.setNum(1);

            JdbcSession.begin();
            try {
                if(dao.updateNotNullAndInc(updateEntity,conditions,"num") > 0) {
                	agentLevel.setCreateDate(new Date());
                    if (dao.insert(agentLevel) > 0) {
                        JdbcSession.commit();
                        return true;
                    }
                }
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            } finally {
                JdbcSession.end();
            }
        }else{
            agentLevel.setCreateDate(new Date());
            try {
                return dao.insert(agentLevel) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return false;
    }

    public boolean updateNotNull(AgentLevel agentLevel) throws LogicException {
        try {
        	AgentLevel temp = getByName(agentLevel.getName());
            if(temp != null && !temp.getNum().equals(agentLevel.getNum())){
                throw new LogicException("级别名称已存在");
            }
        	
            return dao.updateNotNull(agentLevel) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }

    public boolean deleteByNum(Integer num) throws LogicException {
        JdbcSession.begin();
        try {
            if(dao.deleteById(num) > 0){
                Conditions conditions = new Conditions(AgentLevel.class);
                conditions.and().ge("num",num);

                AgentLevel updateEntity = new AgentLevel();
                updateEntity.setNum(1);

                if(dao.updateNotNullAndReduce(updateEntity,conditions,"num") >= 0){
                    JdbcSession.commit();
                    return true;
                }
            }
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        } finally {
            JdbcSession.end();
        }
        return false;
    }
}
